IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'tcc')
	DROP DATABASE [tcc]
GO 

CREATE DATABASE [tcc]  ON (NAME = N'tcc_Data', FILENAME = N'C:\Arquivos de programas\Microsoft SQL Server\MSSQL\data\tcc_Data.MDF' , SIZE = 1, FILEGROWTH = 10%) LOG ON (NAME = N'tcc_Log', FILENAME = N'C:\Arquivos de programas\Microsoft SQL Server\MSSQL\data\tcc_Log.LDF' , SIZE = 1, FILEGROWTH = 10%)
 COLLATE Latin1_General_CI_AS
GO

exec sp_dboption N'tcc', N'autoclose', N'false'
GO

exec sp_dboption N'tcc', N'bulkcopy', N'false'
GO

exec sp_dboption N'tcc', N'trunc. log', N'false'
GO

exec sp_dboption N'tcc', N'torn page detection', N'true'
GO

exec sp_dboption N'tcc', N'read only', N'false'
GO

exec sp_dboption N'tcc', N'dbo use', N'false'
GO

exec sp_dboption N'tcc', N'single', N'false'
GO

exec sp_dboption N'tcc', N'autoshrink', N'false'
GO

exec sp_dboption N'tcc', N'ANSI null default', N'false'
GO

exec sp_dboption N'tcc', N'recursive triggers', N'false'
GO

exec sp_dboption N'tcc', N'ANSI nulls', N'false'
GO

exec sp_dboption N'tcc', N'concat null yields null', N'false'
GO

exec sp_dboption N'tcc', N'cursor close on commit', N'false'
GO

exec sp_dboption N'tcc', N'default to local cursor', N'false'
GO

exec sp_dboption N'tcc', N'quoted identifier', N'false'
GO

exec sp_dboption N'tcc', N'ANSI warnings', N'false'
GO

exec sp_dboption N'tcc', N'auto create statistics', N'true'
GO

exec sp_dboption N'tcc', N'auto update statistics', N'true'
GO

use [tcc]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fk1]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE [dbo].[teste_2] DROP CONSTRAINT fk1
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[teste_1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[teste_1]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[teste_2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[teste_2]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[teste_3]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[teste_3]
GO

if not exists (select * from dbo.sysusers where name = N'tcc' and uid < 16382)
	EXEC sp_grantdbaccess N'tcc', N'tcc'
GO

exec sp_addrolemember N'db_accessadmin', N'tcc'
GO

exec sp_addrolemember N'db_backupoperator', N'tcc'
GO

exec sp_addrolemember N'db_datareader', N'tcc'
GO

exec sp_addrolemember N'db_datawriter', N'tcc'
GO

exec sp_addrolemember N'db_ddladmin', N'tcc'
GO

exec sp_addrolemember N'db_denydatareader', N'tcc'
GO

exec sp_addrolemember N'db_denydatawriter', N'tcc'
GO

exec sp_addrolemember N'db_owner', N'tcc'
GO

exec sp_addrolemember N'db_securityadmin', N'tcc'
GO

CREATE TABLE [dbo].[teste_1] (
	[Coluna1] [int] NOT NULL ,
	[Coluna2] [char] (8) COLLATE Latin1_General_CI_AS NULL ,
	[Coluna3] [datetime] NULL ,
	[Coluna4] [binary] (50) NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[teste_2] (
	[Coluna1] [numeric](18, 0) NULL ,
	[Coluna2] [float] NOT NULL ,
	[Coluna3] [binary] (50) NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[teste_3] (
	[Coluna1] [text] COLLATE Latin1_General_CI_AS NOT NULL ,
	[Coluna2] [char] (40) COLLATE Latin1_General_CI_AS NOT NULL ,
	[Coluna3] [datetime] NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[teste_1] WITH NOCHECK ADD 
	CONSTRAINT [DF_teste_1_Coluna1] DEFAULT (3) FOR [Coluna1],
	CONSTRAINT [pk1] PRIMARY KEY  CLUSTERED 
	(
		[Coluna4]
	)  ON [PRIMARY] 
GO

ALTER TABLE [dbo].[teste_2] WITH NOCHECK ADD 
	CONSTRAINT [pk2] PRIMARY KEY  CLUSTERED 
	(
		[Coluna3]
	)  ON [PRIMARY] 
GO

ALTER TABLE [dbo].[teste_2] ADD 
	CONSTRAINT [fk1] FOREIGN KEY 
	(
		[Coluna3]
	) REFERENCES [dbo].[teste_1] (
		[Coluna4]
	)
GO

